import express from 'express'
import config from '../config.json'

const session = require('express-session');
const bodyParser = require('body-parser');

import { Nuxt, Builder } from 'nuxt'
import router from './router';
import api from './api';
import log4js from 'log4js';
import logConfig from './log4j';

const app = express();
const host = process.env.HOST || config.host;
const port = process.env.PORT || config.port;
const numCPUs = require('os').cpus().length
const cluster = require('cluster')


log4js.configure(logConfig);

app.use(log4js.connectLogger(log4js.getLogger('请求'), { level: 'auto' }));

app.use(bodyParser.json());
//app.use(bodyParser.urlencoded({ extended: true }));

app.set('port', port);

app.use(session({
    secret :  'secret', // 对session id 相关的cookie 进行签名
    resave : true,
    saveUninitialized: false, // 是否保存未初始化的会话
    cookie : {
        maxAge : 1000 * 60 * 60 * 30 , // 设置 session 的有效时间，单位毫秒
    },
}));

app.use(api);
app.use(router);


// Import and Set Nuxt.js options
let configNuxt = require('../nuxt.config.js');
config.dev = !(process.env.NODE_ENV === 'production');

// Init Nuxt.js
const nuxt = new Nuxt(configNuxt);

if (config.dev) {
    const builder = new Builder(nuxt);
    builder.build()
}

app.use(nuxt.render);
app.listen(port, host);